from django.db import models
from hospitals.models import HospitalModel, DepartmentModel, IllnessModel
from users.models import UserModel
from Doctor_django.utils.models import BaseModel
from stdimage import StdImageField
from django.utils.safestring import mark_safe
from patients.models import PatientModel


# 标签模型类
class LabelModel(BaseModel):
    name = models.CharField(max_length=255, verbose_name='标签')

    class Meta:
        db_table = 'label_info'
        verbose_name_plural = '标签信息表'

    def __str__(self):
        return self.name


class DoctorModel(models.Model):
    TITLE_CHOICES = {
        (0, '医士'),
        (1, '医师'),
        (2, '主治医师'),
        (3, '副主任医师'),
        (4, '主任医师'),
    }
    STATUS_CHOICES = {
        (0, '休息中'),
        (1, '问诊中'),
        (2, '停止问诊')
    }
    name = models.CharField(max_length=50, verbose_name='医生名称')
    title = models.SmallIntegerField(choices=TITLE_CHOICES, default=0, verbose_name='职称')
    hospital = models.ForeignKey(to=HospitalModel, related_name='doctor_list', on_delete=models.DO_NOTHING, null=True, blank=True, db_constraint=False, verbose_name='所属医院')
    department = models.ForeignKey(to=DepartmentModel, related_name='doctor_list', on_delete=models.DO_NOTHING, null=True, blank=True, db_constraint=False, verbose_name='所属科室')
    avatar = StdImageField(upload_to='avatar/%Y/', default='avatar/2024/default.jpg', null=True, blank=True, verbose_name='头像', variations={
                                       # 'large': (600, 400),
                                       'thumb_800x800': {'width': 800, 'height': 800},
                                       # 'medium': (300, 200),
                                       'thumb_400x400': {'width': 400, 'height': 400},
                                       # 'thumbnail': (100, 100, True),
                                       'thumb_50x50': {'width': 50, 'height': 50},
                                })
    request_time = models.IntegerField(default=5, verbose_name='响应时间')
    profile = models.TextField(null=True, blank=True, verbose_name='个人简介')
    price = models.DecimalField(max_digits=10, decimal_places=2, default=80, verbose_name='诊费')
    illness = models.ManyToManyField(to=IllnessModel, verbose_name='疾病分类')
    label = models.ManyToManyField(to=LabelModel, verbose_name='标签')
    practice_experience = models.TextField(null=True, blank=True, verbose_name='执业经历')
    academic_experience = models.TextField(null=True, blank=True, verbose_name='学术经历')
    professional_qualifications = models.TextField(null=True, blank=True, verbose_name='专业资历')
    second_practice = models.ForeignKey(to=HospitalModel, related_name='doctor2_list', on_delete=models.DO_NOTHING, null=True, blank=True, db_constraint=False,  verbose_name='第二职业医院')
    status = models.SmallIntegerField(choices=STATUS_CHOICES, default=0, verbose_name='问诊状态')

    class Meta:
        db_table = 'doctor_info'
        verbose_name_plural = '医生信息表'

    def __str__(self):
        return self.name

    def avatar_small(self):
        # 小图展示
        return mark_safe(f'<img alt="" src="{self.avatar.thumb_50x50.url}"></img>')

    avatar.short_description = '医生个人头像'
    # 图片是否显示
    avatar.allow_tags = True
    # 设置排序顺序
    avatar.admin_order_field = "avatar"


# 回答量模型类
class AnswerModel(models.Model):
    user_content = models.TextField(verbose_name='用户提问内容')
    doctor_content = models.TextField(null=True, blank=True, verbose_name='医生回答内容')
    user = models.ForeignKey(to=UserModel,  related_name='answer_list', on_delete=models.DO_NOTHING, null=True, blank=True, db_constraint=False, verbose_name='用户')
    doctor = models.ForeignKey(to=DoctorModel,  related_name='answer_list', on_delete=models.DO_NOTHING, null=True, blank=True, db_constraint=False, verbose_name='医生')
    create_time = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    update_time = models.DateTimeField(auto_now=True, verbose_name='回答时间')
    patients = models.ForeignKey(to=PatientModel, related_name='answer_list', on_delete=models.DO_NOTHING, null=True, blank=True, db_constraint=False, verbose_name='患者')
    is_public = models.BooleanField(default=0, verbose_name='是否公开')

    class Meta:
        db_table = 'answer_info'
        verbose_name_plural = '回答量信息表'

    def __str__(self):
        return self.user_content


class AnswerImageModel(models.Model):
    image = StdImageField(upload_to='answer/%Y/', null=True, blank=True, verbose_name='患者病例图片')
    answer = models.ForeignKey(to=AnswerModel, related_name='answer_list', on_delete=models.DO_NOTHING, null=True, blank=True, db_constraint=False, verbose_name='患者')
    class Meta:
        db_table = 'answer_image'
        verbose_name_plural = '患者病例图片信息表'

    def image_small(self):
        # 小图展示
        return mark_safe(f'<img alt="" src="{self.image.thumb_50x50.url}"></img>')

    image.short_description = '医生个人头像'
    # 图片是否显示
    image.allow_tags = True
    # 设置排序顺序
    image.admin_order_field = "image"


# 处方量信息表
class PrescriptionModel(models.Model):
    content = models.TextField(verbose_name='处方内容')
    user = models.ForeignKey(to=UserModel, related_name='prescription_list', on_delete=models.DO_NOTHING, null=True,
                             blank=True, db_constraint=False, verbose_name='用户')
    doctor = models.ForeignKey(to=DoctorModel, related_name='prescription_list', on_delete=models.DO_NOTHING, null=True,
                               blank=True, db_constraint=False, verbose_name='医生')
    patients = models.ForeignKey(to=PatientModel, related_name='prescription_list', on_delete=models.DO_NOTHING, null=True,
                                 blank=True, db_constraint=False, verbose_name='患者')
    create_time = models.DateTimeField(auto_now_add=True, verbose_name='处方时间')

    class Meta:
        db_table = 'prescription_info'
        verbose_name_plural = '处方量信息表'

    def __str__(self):
        return self.content


# 评价模型类
class EvaluateModel(models.Model):
    content = models.TextField(verbose_name='评价内容')
    star = models.DecimalField(max_digits=3, decimal_places=2, default=0, verbose_name='好评星级')
    user = models.ForeignKey(to=UserModel, related_name='evaluate_list', on_delete=models.DO_NOTHING, null=True,
                             blank=True, db_constraint=False, verbose_name='用户')
    doctor = models.ForeignKey(to=DoctorModel, related_name='evaluate_list', on_delete=models.DO_NOTHING, null=True,
                               blank=True, db_constraint=False, verbose_name='医生')
    create_time = models.DateTimeField(auto_now_add=True, verbose_name='评价时间')

    class Meta:
        db_table = 'evaluate_info'
        verbose_name_plural = '评价信息表'

    def __str__(self):
        return self.content



